package com.example.sfm.controller.teacher;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.TeachersDto;
import com.example.sfm.pojo.entity.Teachers;
import com.example.sfm.pojo.vo.TeachersVo;
import com.example.sfm.result.Result;
import com.example.sfm.service.ITeachersService;
import com.example.sfm.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@RestController("teacherTeachersController")
@RequestMapping("/user/teacher/teachers")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "教师信息管理", description = "教师自身信息的操作接口")
public class TeachersController {
    private final ITeachersService teachersService;

    /**
     * 更新教师信息
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "更新教师信息", description = "根据教师 ID 更新教师的相关信息")
    public Result<?> updateTeacher(@Parameter(description = "教师 ID",in = ParameterIn.PATH) @PathVariable Integer id,
                                   @Parameter(description = "教师信息更新参数") @RequestBody @Validated TeachersDto dto) {
        log.info("更新教师信息: ID={}", id);
        teachersService.updateTeacher(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @GetMapping("/info")
    @Operation(summary = "获取教师信息", description = "根据当前登录的教师 ID 获取教师信息")
    public Result<?> info() {
        log.info("教师信息");
        Integer id = ThreadLocalUtil.getId();
        return Result.success(BeanUtil.copyProperties(teachersService
                .getOne(new LambdaQueryWrapper<Teachers>()
                        .eq(Teachers::getUserId, id)), TeachersVo.class), MessageConstants.SELECT_OK);
    }
}